<?php
// +----------------------------------------------------------------------
// | Author: July
// +----------------------------------------------------------------------
// | 创建时间: 2023-12-27 20:39:42
// +----------------------------------------------------------------------
// | 菜单模型
// +----------------------------------------------------------------------

namespace july\julyadmin\common\model;

use think\Model;

class Menu extends Model
{
    //主键
    protected $pk = 'id';
    //设置自动时间写入类型
    protected $autoWriteTimestamp = 'datetime';
    //设置创建时间字段
    protected $createTime = 'create_time';
    //设置更新时间字段
    protected $updateTime = 'update_time';

    //权限菜单tree数据
    private $permissionsTree = [];

    /**
     * 上级菜单
     * @Author   July
     * @DateTime 2024-01-13T20:13:44+0800
     */
    public function parentmenu()
    {
        return $this->hasOne(Menu::class, 'id', 'parent_id');
    }

    /**
     * 删除数据后事件更新缓存
     * @Author   July
     * @DateTime 2024-01-13T20:18:44+0800
     */
    public static function onAfterDelete($data)
    {
        \july\julyadmin\common\model\AdminRoleList::where([['', 'exp', \think\facade\Db::raw("FIND_IN_SET(menu_parent_ids, '{$data['id']}')")]])->delete();
    }

    /**
     * 获取菜单列表
     * @Author   July
     * @DateTime 2024-01-13T20:14:17+0800
     * @param  integer $parent_id 父id
     * @param  array   $where     查询条件
     */
    public static function menuTree($parent_id = 0, $where = [])
    {
        if (session('admininfo.roleid') == 1) {
            $order = 'id ASC';
            $data  = self::where(['disabled' => 1, 'parent_id' => $parent_id])->where($where)->order($order)->select()->toArray();
            if ($data && is_array($data)) {
                foreach ($data as &$arr) {
                    $arr['url'] = url($arr['controller'] . '/' . $arr['action']);
                    $arr['children'] = self::menuTree($arr['id'], $where);
                }
            }
        } else {
            $order = 'menu.id ASC';
            $data  = self::alias('menu')->field('menu.*')->join('admin_role_list L', 'FIND_IN_SET(menu.id,L.menu_parent_ids) OR menu.id=L.menu_id')->where(['menu.disabled' => 1, 'menu.parent_id' => $parent_id, 'L.role_id' => session('admininfo.roleid')])->group('menu.id')->order($order)->select()->toArray();
            if ($data && is_array($data)) {
                foreach ($data as &$arr) {
                    $arr['url'] = url($arr['controller'] . '/' . $arr['action']);
                    $arr['children'] = self::menuTree($arr['id'], $where);
                }
            }
        }

        return $data;
    }

    /**
     * 获取指定菜单所有上级id
     * @Author   July
     * @DateTime 2024-01-13T20:14:34+0800
     * @param  string $menu_id 菜单id
     * @return array  所有上级id一维数组
     */
    public static function getParentIds($menu_id = '', &$ids = [])
    {
        $parent_id = self::where(['id' => $menu_id])->value('parent_id');
        if ($parent_id) {
            $ids[] = $parent_id;
            self::getParentIds($parent_id, $ids);
        }
        return $ids;
    }

    /**
     * 获取权限按钮所有上级菜单id
     * @Author   July
     * @DateTime 2024-01-13T20:14:51+0800
     * @param  string $router      权限按钮路由，如admin/Content/add
     * @param  string $permissions 当前便利到的权限array
     * @param  string $ids         所有上级菜单id一维数组
     * @return array               所有上级菜单id一维数组
     */
    public static function getButtonParentMenuIds($router = '', $permissions = [], &$ids = [])
    {
        foreach ($permissions as $pk => $pv) {
            if ($pv['id'] == $router) {
                $ids[] = $pv['pid'];
                $ids   = array_merge($ids, self::getParentIds($pv['pid']));
                return $ids;
            } else {
                if (isset($pv['children']) && $pv['children']) {
                    self::getButtonParentMenuIds($router, $pv['children'], $ids);
                }
            }
        }
        return $ids;
    }

    /**
     * 角色权限
     * @Author   July
     * @DateTime 2024-01-13T20:15:07+0800
     * @return   [type]                   [description]
     */
    public function adminrolelist()
    {
        return $this->hasOne(AdminRoleList::class, 'menu_id', 'id');
    }
}
